﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Spine.Unity;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;

public class MainUI : MonoBehaviour
{
    public GameObject skinToggleTemplate;
    public GameObject animToggleTemplate;
    public GameObject slotToggleTemplate;
    public Button loadSpientButton;
    public Text spineNameText;
    public InputField windowAlphaInput, frameRateInput;
    [HideInInspector]public GraphicRaycaster graphicRaycaster;

    public void Init()
    {
        EventBus.Instance.AddEventHandler(EventID.OnOneSpineLoaded, OnOneSpineLoaded);
        EventBus.Instance.AddEventHandler<GameObject>(EventID.OnOneSpineSelected, OnOneSpineSelected);
        loadSpientButton.onClick.AddListener(OnLoadSpineClick);
        graphicRaycaster = GetComponent<GraphicRaycaster>();
        SetWindowAlpha();
        SetAppFramerate();
    }

    private void OnOneSpineLoaded()
    {
        RefreshSpineData();
    }

    private void OnOneSpineSelected(GameObject go)
    {
        RefreshSpineData();
    }

    public void RefreshSpineData()
    {
        spineNameText.text = SpineCreateControl.Instance.CurrentSpineConfigData.name;
        SetSpineSlots();
        SetSkins();
        SetAnims();
    }

    private void OnLoadSpineClick()
    {
        string spinePath = FileDialog.GetOpenFileName("json (*.json)|skel(*.skel)", "选择spine文件", "D:/");
        SpineConfigData configData = new SpineConfigData() { jsonPath = spinePath };
        SpineCreateControl.Instance.LoadTheSpine(configData);
    }

    private void SetAppFramerate()
    {
        var frameRate = AppMgr.Instance.appConfig.frameRate;
        frameRateInput.text = frameRate.ToString();
        Application.targetFrameRate = frameRate;

        frameRateInput.onEndEdit.AddListener((str)=>{
            AppMgr.Instance.appConfig.frameRate = int.Parse(str);
            Application.targetFrameRate = int.Parse(str);
        });
    }

    private void SetWindowAlpha()
    {
        windowAlphaInput.text = AppMgr.Instance.appConfig.alpha.ToString();
        windowAlphaInput.onEndEdit.AddListener((str)=>{
            WinSetting.Instance.WinAlpha = int.Parse(str);
        });
    }

    private void SetSkins()
    {
        Utils.DestroyChildObjects(skinToggleTemplate.transform.parent, new List<string>() { "Toggle" });

        var skins = SpineCreateControl.Instance.CurrentSpineShowData.skins;
        if (skins.Count > 0)
        {
            foreach (var item in skins)
            {
                GameObject skinObj = Instantiate<GameObject>(skinToggleTemplate, skinToggleTemplate.transform.parent);
                skinObj.SetActive(true);
                string skinName = item.Name;
                skinObj.GetComponentInChildren<Text>().text = skinName;
                var toggle = skinObj.GetComponent<Toggle>();
                toggle.isOn = SpineCreateControl.Instance.CurrentSpineConfigData.skinName == skinName;
                toggle.onValueChanged.AddListener((value) =>
                {
                    SpineCreateControl.Instance.SetSkin(skinName);
                });
            }
        }
    }

    private void SetAnims()
    {
        Utils.DestroyChildObjects(animToggleTemplate.transform.parent, new List<string>() { "Toggle" });

        var anims = SpineCreateControl.Instance.CurrentSpineShowData.anims;
        if (anims.Count > 0)
        {
            foreach (var item in anims)
            {
                GameObject animObj = Instantiate<GameObject>(animToggleTemplate, animToggleTemplate.transform.parent);
                animObj.SetActive(true);
                string animName = item.Name;
                animObj.GetComponentInChildren<Text>().text = animName;
                var toggle = animObj.GetComponent<Toggle>();
                toggle.isOn = SpineCreateControl.Instance.CurrentSpineConfigData.animName == animName;
                toggle.onValueChanged.AddListener((value) =>
                {
                    SpineCreateControl.Instance.SetAnim(animName);
                });
            }
        }
    }

    private void SetSpineSlots()
    {
        Utils.DestroyChildObjects(slotToggleTemplate.transform.parent, new List<string>() { "Toggle" });

        var slots = SpineCreateControl.Instance.CurrentSpineShowData.slots;
        var slotsHide = SpineCreateControl.Instance.CurrentSpineConfigData.slotHides;
        if (slots.Count > 0)
        {
            foreach (var item in slots)
            {
                GameObject slotObj = Instantiate<GameObject>(slotToggleTemplate, slotToggleTemplate.transform.parent);
                slotObj.SetActive(true);
                string slotName = item.Data.Name;
                slotObj.GetComponentInChildren<Text>().text = slotName;
                var toggle = slotObj.GetComponent<Toggle>();
                toggle.isOn = !(slotsHide.Count > 0 && slotsHide.Contains(slotName));
                toggle.onValueChanged.AddListener((value) =>
                {
                    SpineCreateControl.Instance.SetSlotVisible(slotName, value);
                });
            }
        }
    }
}
